# RestfulThreadMessages
module RestfulThreadMessages
  def self.included(base)
    base.extend(ClassMethods)
  end
  
  module ClassMethods
    def restful_thread_messages
      has_many :inbox_threads, :class_name => 'InboxItem', :foreign_key => 'user_id', :group => 'thread_id', :conditions => { :inbox_deleted => nil }, :order => 'created_at DESC'
      has_many :outbox_threads, :class_name => 'OutboxItem', :foreign_key => 'user_id', :group => 'thread_id', :conditions => { :outbox_deleted => nil }, :order => 'created_at DESC'
      has_many :unread_threads, :class_name => 'InboxItem', :foreign_key => 'user_id', :group => 'thread_id', :order => 'created_at DESC', :conditions => { :read_at => nil }
      has_many :box_items, :class_name => 'BoxItem', :foreign_key => 'user_id', :order => 'created_at'
      has_many :inbox_items, :class_name => 'BoxItem', :foreign_key => 'user_id', :conditions => { :inbox_deleted => nil }, :order => 'created_at'
      has_many :outbox_items, :class_name => 'BoxItem', :foreign_key => 'user_id', :conditions => { :outbox_deleted => nil }, :order => 'created_at'
      has_many :inbox_messages, :through => :inbox_items, :source => :message
      has_many :outbox_messages, :through => :outbox_items, :source => :message
    end
  end

end
